﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Think9.Models;
using Think9.Services.Basic;

namespace Think9.Controllers.Basic
{
    public class AAAController : BaseController
    {
        public SortService SortService = new SortService();

        //IHostingEnvironment env;
        //public HomeController(IHostingEnvironment _env) { env = _env; }

        //public IActionResult Index()
        //{
        //    //https://localhost:44359/home/index
        //    string url = HttpContext.Request.GetDisplayUrl();

        //    //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI\\bin\\Debug\\netcoreapp2.1\\
        //    string path = System.AppContext.BaseDirectory;

        //    //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI
        //    string siteRoot = env.ContentRootPath;

        //    //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI\\wwwroot
        //    string wwwroot = env.WebRootPath;

        //    //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI\\bin\\Debug\\netcoreapp2.1
        //    var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);

        //    return View();
        //}

        // GET: Permissions/Role
        public override ActionResult Index(int? id)
        {
            base.Index(id);
            return View();
        }

        [HttpGet]
        public JsonResult GetList(SortEntity model, PageInfoEntity pageInfo)
        {
            string classid = HttpContext.Request.Query["classid"].ToString();
            pageInfo.field = "SortOrder";
            var result = SortService.GetPageByFilter(model, pageInfo, "where classid='" + classid + "'");
            return Json(result);
        }

        public ActionResult Add(string classid)
        {
            ViewBag.ClassID = classid;
            return View();
        }

        [HttpPost]
        public ActionResult Add(SortEntity model, string classid)
        {
            model.ClassID = classid;

            string where = "where ClassID=@ClassID and SortID=@SortID";
            object param = new { ClassID = model.ClassID, SortID = model.SortID };
            if (SortService.GetTotal(where, param) > 0)
            {
                var result = ErrorTip("添加失败!已存在相同分类编码");
                return Json(result);
            }
            else
            {
                var result = SortService.Insert(model) ? SuccessTip("操作成功") : ErrorTip("操作失败");
                return Json(result);
            }
        }

        public ActionResult Edit(string classid, string sortid)
        {
            ViewBag.ClassID = classid;
            string where = "where ClassID=@ClassID and SortID=@SortID";
            object param = new { ClassID = classid, SortID = sortid };
            var model = SortService.GetByWhereFirst(where, param);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(SortEntity model, string classid)
        {
            string where = "where ClassID='" + classid + "' and SortID='" + model.SortID + "'";
            string updateFields = "SortName,SortOrder";

            var result = SortService.UpdateByWhere(where, updateFields, model) > 0 ? SuccessTip("操作成功") : ErrorTip("编辑失败");
            return Json(result);
        }

        [HttpGet]
        public JsonResult Delete(int id)
        {
            var result = ButtonService.DeleteById(id) ? SuccessTip("删除成功") : ErrorTip("操作失败");
            return Json(result);
        }
    }
}